package com.dabai.qrtools.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.dabai.qrtools.R;
import com.dabai.qrtools.adapter.ColorChooseAdapter;
import com.dabai.qrtools.utils.DabaiUtils;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.textfield.TextInputLayout;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * Description : 对MDC弹窗的更多界面封装
 *
 * @author BAI
 */
public class MdcDialog {


    public static AlertDialog copyDialog(Context context,String title,String text){
        return new MaterialAlertDialogBuilder(context)
                .setTitle(title)
                .setMessage(text)
                .setPositiveButton("确认",null)
                .setNeutralButton("复制", (dialog, which) -> {
                    DabaiUtils.copyText(context,text);
                }).show();
    }

    public interface OnInputDialog2ButtonListener {
        /**
         * 确认
         *
         * @param dialog   对话框
         * @param content1 content1
         * @param content2 content2
         */
        void confirm(AlertDialog dialog, String content1,String content2);

        /**
         * 取消
         */
        void cancel();

    }

    /**
     * 显示输入对话框
     * 确认时,请手动dismiss
     *
     * @param context                      上下文
     * @param title                        标题
     * @param hint1                        hint1
     * @param hint2                        提示2
     * @param onInputDialog2ButtonListener 在输入dialog2按钮侦听器
     * @return {@link AlertDialog}
     */
    public static AlertDialog showInputDialog2(@NonNull Context context, @NonNull String title,@NonNull String defaultText1,@NonNull String defaultText2, @NonNull String hint1,@NonNull String hint2, OnInputDialog2ButtonListener onInputDialog2ButtonListener) {

        View dialogView = View.inflate(context, R.layout.dialog_input_2, null);
        TextView textView = dialogView.findViewById(R.id.titleView);

        TextInputLayout til1 = dialogView.findViewById(R.id.til1);
        til1.setHint(hint1);
        til1.getEditText().setText(defaultText1);

        TextInputLayout til2 = dialogView.findViewById(R.id.til2);
        til2.setHint(hint2);
        til2.getEditText().setText(defaultText2);

        textView.setText(title);

        AlertDialog alertDialog = new MaterialAlertDialogBuilder(context)
                .setView(dialogView)
                .setNeutralButton("取消", (dialog, which) -> onInputDialog2ButtonListener.cancel())
                .setPositiveButton("确认", null).show();

        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(view -> {
            String content1 = Objects.requireNonNull(til1.getEditText()).getText().toString();
            String content2 = Objects.requireNonNull(til2.getEditText()).getText().toString();
            onInputDialog2ButtonListener.confirm(alertDialog, content1,content2);
        });

        return alertDialog;
    }



    public interface OnConfirmDialogButtonListener {
        /**
         * 确认
         */
        void confirm();

        /**
         * 取消
         */
        void cancel();
    }

    /**
     * 显示确认对话框
     *
     * @param context                       上下文
     * @param title                         标题
     * @param content                       内容
     * @param onConfirmDialogButtonListener 在确认对话框按钮侦听器
     * @return {@link AlertDialog}
     */
    public static AlertDialog showConfirmDialog(@NonNull Context context,@NonNull String title, @NonNull String content, OnConfirmDialogButtonListener onConfirmDialogButtonListener) {

        return new MaterialAlertDialogBuilder(context)
                .setTitle(title)
                .setMessage(content)
                .setPositiveButton("确认", (dialog, which) -> onConfirmDialogButtonListener.confirm())
                .setNeutralButton("取消", (dialog, which) -> onConfirmDialogButtonListener.cancel())
                .show();

    }



    public interface OnInputDialogButtonListener {
        /**
         * 确认
         *
         * @param dialog  对话框
         * @param content 内容
         */
        void confirm(DialogInterface dialog, String content);

        /**
         * 开始
         *
         * @param dialog          对话框
         * @param textInputLayout 文本输入布局
         */
        void start(DialogInterface dialog, TextInputLayout textInputLayout);

        /**
         * 取消
         */
        void cancel();

    }


    public interface OnSeekbarDialogButtonListener {
        /**
         * 确认
         *
         * @param dialog   对话框
         * @param progress 进步
         */
        void confirm(DialogInterface dialog, int progress);

        /**
         * 取消
         */
        void cancel();

    }

    /**
     * 显示seekbar对话框
     * 确认时,请手动dismiss
     *
     * @param context                     上下文
     * @param title                       标题
     * @param onInputDialogButtonListener 输入对话框按钮侦听器
     * @param maxProgress                 马克斯进展
     * @param progress                    进步
     * @return {@link AlertDialog}
     */
    public static AlertDialog showSeekbarDialog(@NonNull Context context,@NonNull String title, @NonNull int maxProgress,@NonNull int progress, OnSeekbarDialogButtonListener onInputDialogButtonListener) {

        View dialogView = View.inflate(context, R.layout.dialog_seekbar, null);
        TextView textView1 = dialogView.findViewById(R.id.textView1);
        TextView textView2 = dialogView.findViewById(R.id.textView2);
        SeekBar seekBar = dialogView.findViewById(R.id.seekBar);

        seekBar.setMax(maxProgress);
        seekBar.setProgress(progress);

        textView2.setText("最大值:"+maxProgress);
        textView1.setText(""+progress);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                textView1.setText(""+progress);
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        AlertDialog alertDialog = new MaterialAlertDialogBuilder(context)
                .setTitle(title)
                .setView(dialogView)
                .setNeutralButton("取消", (dialog, which) -> onInputDialogButtonListener.cancel())
                .setPositiveButton("确认", null).show();

        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(view -> {
            onInputDialogButtonListener.confirm(alertDialog, seekBar.getProgress());
        });

        return alertDialog;
    }


    /**
     * 显示输入对话框
     * 确认时,请手动dismiss
     *
     * @param context                     上下文
     * @param title                       标题
     * @param hint                        提示
     * @param onInputDialogButtonListener 输入对话框按钮侦听器
     * @param defaultText                 默认文本
     * @return {@link AlertDialog}
     */
    public static AlertDialog showInputDialog(@NonNull Context context,@NonNull String title, @NonNull String defaultText,@NonNull String hint, OnInputDialogButtonListener onInputDialogButtonListener) {

        View dialogView = View.inflate(context, R.layout.dialog_input, null);
        TextView textView = dialogView.findViewById(R.id.titleView);
        TextInputLayout til1 = dialogView.findViewById(R.id.til1);
        til1.setHint(hint);

        if (!defaultText.isEmpty()) {
            Objects.requireNonNull(til1.getEditText()).setText(defaultText);
        }

        textView.setText(title);

        AlertDialog alertDialog = new MaterialAlertDialogBuilder(context)
                .setView(dialogView)
                .setNeutralButton("取消", (dialog, which) -> onInputDialogButtonListener.cancel())
                .setPositiveButton("确认", null).show();

        onInputDialogButtonListener.start(alertDialog,til1);


        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(view -> {
            String content = Objects.requireNonNull(til1.getEditText()).getText().toString();
            onInputDialogButtonListener.confirm(alertDialog, content);
        });

        return alertDialog;
    }



    /**
     * 显示输入对话框
     * 确认时,请手动dismiss
     *
     * @param context                     上下文
     * @param title                       标题
     * @param hint                        提示
     * @param onInputDialogButtonListener 输入对话框按钮侦听器
     * @param defaultText                 默认文本
     * @return {@link AlertDialog}
     */
    public static AlertDialog showInputDialogAndPaste(@NonNull Context context,@NonNull String title, @NonNull String defaultText,@NonNull String hint, OnInputDialogButtonListener onInputDialogButtonListener) {

        View dialogView = View.inflate(context, R.layout.dialog_input, null);
        TextView textView = dialogView.findViewById(R.id.titleView);
        TextInputLayout til1 = dialogView.findViewById(R.id.til1);
        til1.setHint(hint);

        if (!defaultText.isEmpty()) {
            Objects.requireNonNull(til1.getEditText()).setText(defaultText);
        }

        textView.setText(title);

        AlertDialog alertDialog = new MaterialAlertDialogBuilder(context)
                .setView(dialogView)
                .setNeutralButton("剪切板导入", (dialog, which) -> {
                    onInputDialogButtonListener.confirm(dialog, DabaiUtils.getClipboardContent(context));
                })
                .setPositiveButton("确认", null).show();

        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(view -> {
            String content = Objects.requireNonNull(til1.getEditText()).getText().toString();
            onInputDialogButtonListener.confirm(alertDialog, content);
        });

        return alertDialog;
    }

    /**
     * 显示搜索对话框
     * 确认时,请手动dismiss
     *
     * @param context                     上下文
     * @param title                       标题
     * @param hint                        提示
     * @param onInputDialogButtonListener 输入对话框按钮侦听器
     * @param defaultText                 默认文本
     * @return {@link AlertDialog}
     */
    public static AlertDialog showSearchDialog(@NonNull Context context,@NonNull String title, @NonNull String defaultText,@NonNull String hint, OnInputDialogButtonListener onInputDialogButtonListener) {

        View dialogView = View.inflate(context, R.layout.dialog_search, null);
        TextView textView = dialogView.findViewById(R.id.titleView);
        TextInputLayout til1 = dialogView.findViewById(R.id.til1);
        til1.setHint(hint);

        if (!defaultText.isEmpty()) {
            Objects.requireNonNull(til1.getEditText()).setText(defaultText);
        }

        textView.setText(title);

        AlertDialog alertDialog = new MaterialAlertDialogBuilder(context)
                .setView(dialogView)
                .setNeutralButton("取消", (dialog, which) -> onInputDialogButtonListener.cancel())
                .setPositiveButton("搜索", null).show();

        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(view -> {
            String content = Objects.requireNonNull(til1.getEditText()).getText().toString();
            onInputDialogButtonListener.confirm(alertDialog, content);
        });

        return alertDialog;
    }





    public interface OnColorChooseListener {
        /**
         * 确认
         *
         * @param dialog    对话框
         * @param color     颜色
         * @param colorText 颜色的文本
         */
        void confirm(AlertDialog dialog,int color, String colorText);

        /**
         * 颜色解析错误
         *
         * @param colorText 颜色的文本
         */
        void parseError(AlertDialog dialog,String colorText);
    }


    /**
     * 显示颜色选择对话框
     *
     * @param context               上下文
     * @param title                 标题
     * @param colors                颜色
     * @param onColorChooseListener 在颜色选择监听器
     * @return {@link AlertDialog}
     */
    public static AlertDialog showColorChooseDialog(@NonNull Context context,@NonNull String title,@NonNull String[] colors,OnColorChooseListener onColorChooseListener){

        List<String> colorList = Arrays.asList(colors);

        View diaColorView = View.inflate(context, R.layout.dialog_custom_colors, null);
        AlertDialog diaColor = new MaterialAlertDialogBuilder(context)
                .setTitle(title)
                .setView(diaColorView)
                .setNeutralButton("关闭",null)
                .show();

        RecyclerView recyclerView = diaColorView.findViewById(R.id.recyclerView);

        recyclerView.setLayoutManager(new GridLayoutManager(context,2));

        ColorChooseAdapter colorChooseAdapter = new ColorChooseAdapter(colorList);
        recyclerView.setAdapter(colorChooseAdapter);

        colorChooseAdapter.setOnItemClickListener((adapter, view, position) -> {

            int color = 0;
            String colorText = colorList.get(position);
            try {
                color = Color.parseColor(colorText);
            } catch (Exception ignored) {
            }

            if (color!=0) {
                onColorChooseListener.confirm(diaColor,color,colorText);
            }else {
                onColorChooseListener.parseError(diaColor,colorText);
            }
        });
        return diaColor;
    }




}
